package com.itheima.reggie.controller;

import com.itheima.reggie.ResultInfo;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    //查询所有
    @GetMapping("/category/findAll")
    public ResultInfo findAll() {
     List<Category> categoryList = categoryService.findAll();
        return ResultInfo.success(categoryList);
    }

    //保存
    @PostMapping("/category")
    public ResultInfo save(@RequestBody Category category) {
        //調用service
        categoryService.save(category);
        //返回结果
        return ResultInfo.success(null);
    }


    //修改
    @PutMapping("/category")
    public ResultInfo update(@RequestBody Category category) {
        //调用service修改
        categoryService.update(category);
        return ResultInfo.success(null);
    }


    //删除
    @DeleteMapping("/category")
    public ResultInfo delete(Long id) {
        //调用service删除
       ResultInfo resultInfo =  categoryService.delete(id);
        //返回结果
        return resultInfo;
    }


    //根据type查询分类信息
    @GetMapping("/category/list")
    public ResultInfo findType(Integer type) {
       List<Category> categoryList =  categoryService.findByType(type);
        return ResultInfo.success(categoryList);
    }
}
